package com.sdry.good.logistics.service;

import com.sdry.common.BaseTypeEnum;
import com.sdry.good.logistics.entity.ReportDeficiencyOrderDetail;
import com.sdry.good.logistics.mapper.ReportPickingMapper;
import com.sdry.good.logistics.entity.ReportPicking;
import com.sdry.product.waitingarea.entity.StayContainerStare;
import com.sdry.untils.UserContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 领料清单
 * @author makejava
 * @since 2020-04-02 15:29:39
 */
@Service("reportPickingService")
public class ReportPickingServiceImpl implements ReportPickingService {
    @Resource
    private ReportPickingMapper reportPickingDao;


    @Override
    public List<ReportPicking> queryReportPickingPage(ReportPicking reportPicking) {
        List<ReportPicking> reportPickings = reportPickingDao.queryReportPickingPage(reportPicking);
        for (ReportPicking picking : reportPickings) {
            String remark = picking.getRemark();
            String nameByCode = BaseTypeEnum.getNameByCode(remark);
            picking.setRemark(nameByCode);
        }
        return reportPickings;
    }

    @Override
    public Long queryReportPickingTotal(ReportPicking reportPicking) {
        return reportPickingDao.queryReportPickingTotal(reportPicking);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param picId 主键
     * @return 实例对象
     */
    @Override
    public ReportPicking queryById(Long picId) {
        return this.reportPickingDao.queryById(picId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<ReportPicking> queryAllByLimit(int offset, int limit) {
        return this.reportPickingDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param reportPicking 实例对象
     * @return 实例对象
     */
    @Override
    public ReportPicking insert(ReportPicking reportPicking) {
        this.reportPickingDao.insert(reportPicking);
        return reportPicking;
    }

    /**
     * 修改数据
     *
     * @param reportPicking 实例对象
     * @return 实例对象
     */
    @Override
    public ReportPicking update(ReportPicking reportPicking) {
        this.reportPickingDao.update(reportPicking);
        return this.queryById(reportPicking.getPicId());
    }

    /**
     * 通过主键删除数据
     *
     * @param picId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long picId) {
        return this.reportPickingDao.deleteById(picId) > 0;
    }


    /**
     * 条件查询数据(permissionType传值必须传remark才起作用——X)
     * @param param 实体条件
     * @return 实体集合
     */
    public List<ReportPicking> queryAllByMution(ReportPicking param){
        return reportPickingDao.queryAllByMution(param);
    }
    /**
     * 条件查询清单编号(permissionType传值必须传remark才起作用——X)
     * @param param 实体条件
     * @return 实体集合
     */
    public List<ReportPicking> queryPiNumberAllByMution(ReportPicking param){
        return reportPickingDao.queryPiNumberAllByMution(param);
    }
    /**
     *	判断成品链是否满足接收的领料信息返回不满足的领料信息
     * @param ids 领料信息id集合
     * @param nums 领料信息数量集合（与id对应）
     * @param code 扫码集合 用","分割
     * @return 不满足的领料信息
     */
    public List<ReportPicking> isSatisfyReportPickingsByPicIds(String ids,String nums,String code) {
        return reportPickingDao.isSatisfyReportPickingsByPicIds(ids,nums,code);
    }
    /**
     *	成品链或成品库根据接收的领料信息进行解绑
     * @param ids 领料信息id集合
     * @param nums 领料信息数量集合（与id对应）
     * @param oid 出库单id
     * @param code 扫码集合 用","分割
     * @return 解绑领料信息数
     */
    public Integer  outReportPickingsByPicIds(String ids,String nums,Long oid,String code){
        return reportPickingDao.outReportPickingsByPicIds(ids,nums,oid,code);
    }
    /**
     * 根据条件查询待发货信息
     * @param stayContainerStare 条件
     * @return
     */
    public List<StayContainerStare> queryStayContainerStareByMution(StayContainerStare stayContainerStare){
        return reportPickingDao.queryStayContainerStareByMution(stayContainerStare);
    }

    @Override
    public List<ReportDeficiencyOrderDetail> queryReportDeficiencyOrder(Long rdoId) {
        return reportPickingDao.queryReportDeficiencyOrder(rdoId);
    }

    @Override
    public void addReportPicking(Long rdoId) {
        String numberCP = String.valueOf(System.currentTimeMillis());
        String numberCPK = String.valueOf(System.currentTimeMillis());
        String numberDZ = String.valueOf(System.currentTimeMillis());
        //报缺单的物料
        List<ReportDeficiencyOrderDetail> reportDeficiencyOrderDetails = reportPickingDao.queryReportDeficiencyOrder(rdoId);
        for (ReportDeficiencyOrderDetail reportDeficiencyOrderDetail : reportDeficiencyOrderDetails) {
            //需求数量
            Long rdodNum = Long.valueOf(reportDeficiencyOrderDetail.getRdodNum());
            //成品链
            Long numCP = reportPickingDao.queryCPStock(reportDeficiencyOrderDetail.getMaId());
            //成品库
            Long numCPK = reportPickingDao.queryCPKtock(reportDeficiencyOrderDetail.getMaId());
            //待转区库存
            Long numDZ = reportPickingDao.queryDZtock(reportDeficiencyOrderDetail.getMaId());
            if (numDZ == null) numDZ = 0L;
            if (numCP == null) numCP = 0L;
            if (numCPK == null) numCPK = 0L;
            if (rdodNum == null) rdodNum = 0L;
            long tempNum = numCP + numCPK + numDZ;
            //1.需求数量大于库存,//2.需求等于库存（全出）（有多少出多少）
            if (tempNum <= rdodNum){
                add(reportDeficiencyOrderDetail,numCP,"CP",numberCP);
                add(reportDeficiencyOrderDetail,numCPK,"CPK",numberCPK);
                add(reportDeficiencyOrderDetail,numDZ,"DZ",numberDZ);
            }else {
                //需求小于库存（出其中最大的数量）
                if (numCP > numCPK){
                    if (numCP > numDZ) {
                        //cp最大
                        add(reportDeficiencyOrderDetail,numCP,"CP",numberCP);
                    }else {
                        //numDZ 最大
                        add(reportDeficiencyOrderDetail,numDZ,"DZ",numberDZ);
                    }
                }else {
                    if (numCPK > numDZ) {
                        //numCPK 最大
                        add(reportDeficiencyOrderDetail,numCPK,"CPK",numberCPK);
                    }else {
                        //numDZ 最大
                        add(reportDeficiencyOrderDetail,numDZ,"DZ",numberDZ);
                    }
                }
            }
        }
        //更改报缺单状态为1
        Long userId = UserContext.getSessionUserIdByServiceImpl();
        int effect = reportPickingDao.updateReportDeficiencyOrder(rdoId,userId);
    }

    @Override
    public boolean addReportPickingManual(String quantitys, String maIds, String remark1, String plNumber) {
        String[] strIds = maIds.split(",");
        String[] quantity = quantitys.split(",");
        for (int i = 0; i < strIds.length; i++) {
            ReportPicking reportPicking = new ReportPicking();
            //编号
            reportPicking.setPiNumber(plNumber);
            //物料ID
            reportPicking.setMaterielId(Long.parseLong(strIds[i]));
            reportPicking.setQuantity(Long.parseLong(quantity[i]));
            //报缺单详细，-1 手动生成
            reportPicking.setRdodId(-1L);
            reportPicking.setCreateBy(UserContext.getSessionUserIdByServiceImpl().toString());
            //出库位置
            reportPicking.setRemark(remark1);
            reportPickingDao.insert(reportPicking);
        }

        return true;
    }

    @Override
    public Integer delReportPicking(Long picId) {
        ReportPicking reportPicking = new ReportPicking();
        reportPicking.setPicId(picId);
        reportPicking.setDelFlag("2");
        int updateById = reportPickingDao.updateById(reportPicking);
        return updateById;
    }


    public int add(ReportDeficiencyOrderDetail reportDeficiencyOrderDetail,Long quantity,String remark,String number) {
        ReportPicking reportPicking = new ReportPicking();
        //清单编号
        reportPicking.setPiNumber(number);
        //产品id
        reportPicking.setMaterielId(reportDeficiencyOrderDetail.getMaId());
        //产品数量
        reportPicking.setQuantity(quantity);
        //报缺详细ID
        reportPicking.setRdodId(reportDeficiencyOrderDetail.getRdodId());
        //领料人
        reportPicking.setCreateBy(UserContext.getSessionUserIdByServiceImpl().toString());
        //备注
        reportPicking.setRemark(remark);
        return reportPickingDao.insert(reportPicking);
    }
}